package com.itheima.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.common.BaseContext;
import com.itheima.common.R;
import com.itheima.entity.ShoppingCart;
import com.itheima.service.IShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
@Slf4j
public class ShoppingCartController {

//    注入购物车业务层
    @Autowired
    private IShoppingCartService shoppingCartService;


//      接口二：查询购物车
//      请求url地址：/shoppingCart/list
//      请求方式：GET
//      请求参数：无
    @GetMapping("/list")
    public R<List<ShoppingCart>> list() {
        Long currentId = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId,currentId);
        wrapper.orderByAsc(ShoppingCart::getUserId);
        List<ShoppingCart> list = shoppingCartService.list(wrapper);
        return R.success(list);//"data": [] 购物车列表数据 List<ShoppingCart>
    }

//      接口一：添加购物车
//      请求url地址：/shoppingCart/add
//      请求方式：POST
//      请求参数：JSON格式的数据，
    @PostMapping("/add")
    public R<ShoppingCart> save(@RequestBody ShoppingCart shoppingCart){
//        参数中，要么包含dish要么包含一个setmeal。"dishId": "1397849739276890114","setmealId": null,

//        此处需要对两个内容进行操作
//        当前操作的用户ID："userId": "1417012167126876162",
//        当前购物车菜品数量："number": 14,

//        判断当前购物车中有没有这个菜品或者套餐
        Long currentId = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId,currentId);
        wrapper.eq(shoppingCart.getDishId()!=null,ShoppingCart::getDishId,shoppingCart.getDishId());
        wrapper.eq(shoppingCart.getSetmealId()!=null,ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        wrapper.orderByAsc(ShoppingCart::getUserId);
        ShoppingCart cart = shoppingCartService.getOne(wrapper);

//        执行添加操作
        shoppingCart.setUserId(BaseContext.getCurrentId());
        if (cart==null){
            shoppingCart.setNumber(1);
            shoppingCartService.save(shoppingCart);
        }else {
            cart.setNumber(cart.getNumber()+1);
            shoppingCart = cart;
            shoppingCartService.updateById(cart);
        }
        log.info("shoppingCart：{}",shoppingCart);

        return R.success(shoppingCart);
    }

//      接口三：取消购物车
//      请求url地址：/shoppingCart/sub
//      请求方式：POST
//      请求参数：JSON格式的数据，ShoppingCart接收
    @PostMapping("/sub")
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart){
//        先获取购物车的数据
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        wrapper.eq(shoppingCart.getDishId()!=null,ShoppingCart::getDishId,shoppingCart.getDishId());
        wrapper.eq(shoppingCart.getSetmealId()!=null,ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        wrapper.orderByAsc(ShoppingCart::getUserId);
        ShoppingCart cart = shoppingCartService.getOne(wrapper);

//        对数据进行处理
//        先对数量执行删减操作
        cart.setNumber(cart.getNumber()-1);

//        判断cart中的数量
        if (cart.getNumber()>0){
            shoppingCartService.updateById(cart);
        }else {
            shoppingCartService.removeById(cart);
        }
        return R.success(cart);
    }

//      接口四：清空购物车
//      请求url地址：/shoppingCart/clean
//      请求方式：DELETE
//      请求参数：无
    @DeleteMapping("/clean")
    public R<String> clean(){
//        清空购物车中所有的数据
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        shoppingCartService.remove(wrapper);
        return R.success("清空购物车成功");
    }


}
